package cn.lx.util;


import cn.lx.bean.Question;
import com.google.common.collect.Lists;
import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

public class ExcelUtils {
    public static void main(String[] args) throws Exception {
        InputStream inp = null;
        try {
            String s = "/Users/lixin/Downloads/myexcel.xlsx";

            inp = new FileInputStream(s);
            Workbook wb = WorkbookFactory.create(inp);
            Sheet sheet = wb.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.rowIterator();
            List<Question> list = Lists.newArrayList();
            while (rowIterator.hasNext()) {
                Question question = new Question();
                StringBuilder sb = new StringBuilder();
                Row r = rowIterator.next();
                if (r == null) {
                    System.out.println("Empty Row");
                    continue;
                }
                for (int i = r.getFirstCellNum(); i < r.getLastCellNum(); i++) {
                    Cell cell = r.getCell(i);
                    String cellValue = "";
                    switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            cellValue = cell.getRichStringCellValue().getString();
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(cell)) {
                                cellValue = cell.getDateCellValue().toString();
                            } else {
                                cellValue = String.valueOf(cell.getNumericCellValue());
                            }
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            cellValue = String.valueOf(cell.getBooleanCellValue());
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            cellValue = String.valueOf(cell.getCellFormula());
                            break;
                        case Cell.CELL_TYPE_BLANK:
                            break;
                        default:
                    }
                    if (i == 0) {
                        question.setTopic(cellValue);
                    }
                    if (i == 1) {
                        question.setAnswer(cellValue);
                    }
                    if (i > 1) {
                        sb.append(cellValue).append(";");
                    }

                }
                question.setOption(sb.toString());
                list.add(question);
            }

            System.out.println(list);
            for (Question item : list) {
                System.out.println("题目:" + item.getTopic());
                System.out.println("答案:" + item.getAnswer());
                System.out.println("选项:" + item.getOption());
                System.out.println();
            }


        } finally {
            if (inp != null) {
                inp.close();
            }
        }
    }

}
